<?php
/**
 * Created by PhpStorm.
 * Author:王晓云
 * Date: 2017/12/25 0025
 * Time: 22:00
 */

namespace app\admin\controller;
use think\Db;
use app\admin\model\Product as Prod;
class Product extends Comm
{
    //添加商品类别
   public function addProductCat(){
       if(request()->isPost()){
        $name=input("post.cat_name");
        $pid=input("post.parent_id");
        if($res=Db::name("product_cat")->data(["name"=>$name,"pid"=>$pid])->insert()){
            $this->success("商品类别添加成功！","Product/productCat");
        }else{
            $this->error("商品类别添加失败！");
        }
       }
       $cat=$this->get_cat();
       $this->assign("cat",$cat);
       return $this->fetch();
   }
   //商品类别列表
   public function productCat(){
       $cat=$this->get_cat();
       $this->assign("cat",$cat);
       return $this->fetch();
   }
   //获取无限级分类函数
   public function get_cat($pid=0,&$res=array(),$spa=0){
       $spa=$spa+2;
       $data=\think\Db::name("product_cat")
           ->where("pid",$pid)
           ->select();
       foreach ($data as $v){
          $v['name']=str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$spa)."|--".$v['name'];
          $res[]=$v;
          $this->get_cat($v['id'],$res,$spa);
       }
       return $res;
   }
   //商品列表
    public function product(){
       $product=new Prod;
       $data=$product->paginate(10);
       //dump();
       $this->assign("list",$data);
       return $this->fetch();
    }
    //商品添加
    public function addProduct(){
        if(request()->isPost()){
            $data=input('post.');
           $product=new Prod;
           if($res=$product->allowField(true)->save($data)){
            $this->success("商品添加成功！","Product/product");
           }else{
               $this->error("商品添加失败！");
            }
        }
        $cat=$this->get_cat();
        $this->assign("cat",$cat);
        return $this->fetch();
    }
}
?>